<?php

namespace app\admin\controller\separate_order;

use app\admin\model\Store;
use app\api\model\ShareOrder;
use app\common\controller\Backend;
use think\Request;
use think\Session;

class Accounts extends Backend
{
    protected $searchFields = 'id,name';
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->model = new ShareOrder();
        $this->service_secret = Session::get('admin')['secret'];
        if(!empty($this->service_secret)){
            $this->model->alias("share_order")
                ->where(['share_order.service_secret'=>$this->service_secret]);
        }
    }
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model
                ->with(['storesub','store'])
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);
            foreach ($list->items() as &$item) {
                $item->amount /=  100;
                if ($item->type == "MERCHANT_ID"){
                    $item->storesub->true_name = config('site.company');
                }
            }
            $result = array("total" => $list->total(), "rows" => $list->items());
            return json($result);
        }
        return $this->view->fetch();
    }
}